package com.giibpt;

import java.util.ArrayList;
import java.util.Date;

import com.giibpt.model.HibernateUtil;
import com.giibpt.model.entities.Area;
import com.giibpt.model.entities.Evento;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

public class ConsultaId extends Panel{
	private TextField txtID;
	private Button btnSearch;
	private DateField txtDate;
	private ComboBox cmbArea;
	private Table tblResult;
	private CheckBox chkAll;
	
	public ConsultaId() {
		txtID = new TextField("ID");
		txtID.setRequired(true);
		
		txtDate = new DateField();
		txtDate.setResolution(DateField.RESOLUTION_DAY);
		
		chkAll = new CheckBox("ALL");
		chkAll.setImmediate(true);
		chkAll.addListener(new Property.ValueChangeListener() {
			
			public void valueChange(ValueChangeEvent event) {
				changeState();
			}
		}); 
		
		BeanItemContainer<Area> container = new BeanItemContainer<Area>(Area.class);
		container.addAll(new ArrayList(HibernateUtil.list(Area.class)));
		
		cmbArea = new ComboBox("Area");
		cmbArea.setContainerDataSource(container);
		cmbArea.setItemCaptionPropertyId("nombre");
		cmbArea.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		
		btnSearch = new Button("Buscar");
		btnSearch.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				search();
			}
		});
		
		tblResult = new Table();
		tblResult.setContainerDataSource(new BeanItemContainer<Evento>(Evento.class));
		tblResult.addGeneratedColumn("subarea", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getSubArea().getNombre());
			}
		});
		
		tblResult.addGeneratedColumn("lector", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getIdLector());
			}
		});
		
		tblResult.addGeneratedColumn("tipoEvento.nombre", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getTipoEvento().getNombre());
			}
		});
		
		tblResult.setVisibleColumns(new Object[]{"fecha", "subarea", "lector", "tipoEvento.nombre", });
		
		GridLayout lytMain = new GridLayout(4, 3);
		lytMain.addComponent(txtID, 0,0, 2, 0);
		lytMain.addComponent(btnSearch, 3, 0);
		lytMain.addComponent(chkAll, 0,1);
		lytMain.addComponent(txtDate, 1,1);
		lytMain.addComponent(cmbArea, 2,1);
		lytMain.addComponent(tblResult, 0, 2, 3, 2);
		
		setContent(lytMain);
		
		
	}
	
	private void search(){
		
		if(!txtID.isValid()){
			getWindow().showNotification("El codigo es obligatorio", Notification.TYPE_WARNING_MESSAGE);
			return;
		}
		BeanItemContainer<Evento> container = (BeanItemContainer<Evento>) tblResult.getContainerDataSource();
		container.removeAllItems();
		
		container.addAll(new ArrayList(HibernateUtil.getEventos((String)txtID.getValue(), (Date) txtDate.getValue(), (Area)cmbArea.getValue(), null)));
	}
	
	
	private void changeState(){
		boolean checked = chkAll.booleanValue();
		
		cmbArea.setEnabled(!checked);
		txtDate.setEnabled(!checked);
	}

}
